package com.fyzk.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fyzk.bean.GameInfoBean;
import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * @author Administrator
 * @mood
 * @description 全部 未开赛 进行中 已结束
 * @date 2018/9/18 11:14
 * </p>
 */
public class GameAllAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private LayoutInflater mInflater;
    private List<GameInfoBean> data;
    private boolean isShowEmpty;

    private static final int TYPE_NULL = 0;
    private static final int TYPE_ITEM = 1;
    private static final int TYPE_FOOTER = 2;

    //上拉加载更多状态-默认为0
    private int mLoadMoreStatus = 0;
    //上拉加载更多
    public static final int UP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE = 1;
    //没有更多数据
    public static final int NO_LOAD_MORE = 2;

    public GameAllAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<GameInfoBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public List<GameInfoBean> getData() {
        if (data == null) {
            data = new ArrayList<>();
        }
        return data;
    }

    public void setHeadData(boolean isShowEmpty) {
        this.isShowEmpty = isShowEmpty;
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowEmpty)
            return TYPE_NULL;
        else if (position == data.size())
            return TYPE_FOOTER;
        else
            return TYPE_ITEM;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_NULL) {
            return new GameEmptyHolder(mInflater.inflate(R.layout.item_empty, parent, false));
        } else if (viewType == TYPE_ITEM) {
            return new GameViewHolder(mInflater.inflate(R.layout.item_game_all, parent, false));
        } else {
            return new GameFooterViewHolder(mInflater.inflate(R.layout.item_tab_footer_child1, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof GameEmptyHolder) {
            GameEmptyHolder emptyHolder = (GameEmptyHolder) holder;
            emptyHolder.mTvDesc.setText("比赛就快要开始了哦~");
        } else if (holder instanceof GameViewHolder) {
            final GameViewHolder gameViewHolder = (GameViewHolder) holder;

            if (data != null) {

                if (data.get(position).getType() == 2) {
                    // 未开赛
                    gameViewHolder.mKeyword.setText("未");
                    gameViewHolder.mKeyword.setTextColor(
                            ContextCompat.getColor(context, R.color.color_999999));
                    gameViewHolder.mNotView.setVisibility(View.VISIBLE);
                    gameViewHolder.mScoreText.setVisibility(View.GONE);
                } else if (data.get(position).getType() == 1) {
                    gameViewHolder.mKeyword.setText("中");
                    gameViewHolder.mKeyword.setTextColor(
                            ContextCompat.getColor(context, R.color.top_bg));
                    gameViewHolder.mNotView.setVisibility(View.GONE);
                    gameViewHolder.mScoreText.setVisibility(View.VISIBLE);
                } else if (data.get(position).getType() == 0) {
                    gameViewHolder.mKeyword.setText("完");
                    gameViewHolder.mKeyword.setTextColor(
                            ContextCompat.getColor(context, R.color.top_bg));
                    gameViewHolder.mNotView.setVisibility(View.GONE);
                    gameViewHolder.mScoreText.setVisibility(View.VISIBLE);
                }

                gameViewHolder.mGameType.setText(data.get(position).getEvents() + " " + data.get(position).getPeriod());
                gameViewHolder.mGameTime.setText(data.get(position).getPlayTime());
                gameViewHolder.mMasterClub.setText(data.get(position).getHost());
                Glide.with(context).load(data.get(position).getHostTeamLogo())
                        .placeholder(R.drawable.ic_score_game_default).error(R.drawable.ic_score_game_default)
                        .into(gameViewHolder.mMasterLogo);
                gameViewHolder.mGuestClub.setText(data.get(position).getGuest());
                gameViewHolder.mScoreText.setText(data.get(position).getScore());
                Glide.with(context).load(data.get(position).getGuestTeamLogo())
                        .placeholder(R.drawable.ic_score_game_default).error(R.drawable.ic_score_game_default)
                        .into(gameViewHolder.mGuestLogo);
            }
        } else if (holder instanceof GameFooterViewHolder) {
            GameFooterViewHolder tabFooterViewHolder = (GameFooterViewHolder) holder;

            switch (mLoadMoreStatus) {
                case UP_LOAD_MORE:
                    tabFooterViewHolder.mLoadText.setText("上拉加载更多...");
                    break;
                case LOADING_MORE:
                    tabFooterViewHolder.mLoadLy.setVisibility(View.VISIBLE);
                    tabFooterViewHolder.mLoadText.setText("正在加载更多...");
                    break;
                case NO_LOAD_MORE:
                    tabFooterViewHolder.mLoadLy.setVisibility(View.GONE);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        if (isShowEmpty) {
            return 1;
        } else {
            if (data == null) {
                data = new ArrayList<>();
            }
            return data.size() + 1;
        }
    }

    public class GameEmptyHolder extends RecyclerView.ViewHolder {
        TextView mTvDesc;

        GameEmptyHolder(View itemView) {
            super(itemView);
            mTvDesc = itemView.findViewById(R.id.emptyItem_tvDesc);
        }
    }

    public class GameViewHolder extends RecyclerView.ViewHolder {
        TextView mGameType;
        TextView mGameTime;
        TextView mKeyword;
        TextView mMasterClub;
        ImageView mMasterLogo;
        TextView mGuestClub;
        ImageView mGuestLogo;
        View mNotView;
        TextView mScoreText;

        GameViewHolder(View itemView) {
            super(itemView);
            mGameType = itemView.findViewById(R.id.gameAllItem_gameType);
            mGameTime = itemView.findViewById(R.id.gameAllItem_gameTime);
            mKeyword = itemView.findViewById(R.id.gameAllItem_keyword);
            mMasterClub = itemView.findViewById(R.id.gameAllItem_masterClub);
            mMasterLogo = itemView.findViewById(R.id.gameAllItem_masterLogo);
            mGuestClub = itemView.findViewById(R.id.gameAllItem_guestClub);
            mGuestLogo = itemView.findViewById(R.id.gameAllItem_guestLogo);
            mNotView = itemView.findViewById(R.id.gameAllItem_notView);
            mScoreText = itemView.findViewById(R.id.gameAllItem_scoreText);
        }
    }

    public class GameFooterViewHolder extends RecyclerView.ViewHolder {
        ProgressBar mLoadProgress;
        TextView mLoadText;
        LinearLayout mLoadLy;

        GameFooterViewHolder(View itemView) {
            super(itemView);
            mLoadProgress = itemView.findViewById(R.id.tabFooterUI_loadProgress);
            mLoadText = itemView.findViewById(R.id.tabFooterUI_loadText);
            mLoadLy = itemView.findViewById(R.id.tabFooterUI_loadLy);
        }
    }

    /**
     * 更新加载更多状态
     *
     * @param status
     */
    public void changeMoreStatus(int status) {
        mLoadMoreStatus = status;
        notifyDataSetChanged();
    }

}
